package com.tanhua.app.controller;

import com.tanhua.app.controllerservice.UserService;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class UserInfoController {

    @Autowired
    private UserService userService;


    @GetMapping(name = "回显登录用户数据",value = "/users")
    public ResponseEntity users(Long userID, Long huanxinID){
        // 根据登录用户的id 做userInfo数据的查询

       if (userID != null) {
            // 1、根据userID查询
            return userService.findUserInfo(userID);
        } else if (huanxinID != null) {
            // 2、根据huanxinID查询
            return userService.findUserInfo(huanxinID);
        }else {
           Long userId = UserHolder.getUser().getId();
           return userService.findUserInfo(userId);
       }


    }

    @PostMapping(name = "修改登录用户的头像",value = "/users/header")
    public ResponseEntity header(@RequestHeader("Authorization") String token, MultipartFile headPhoto) throws IOException {
        return userService.head(token,headPhoto);
    }

    @PutMapping(name = "修改登录用户的信息",value = "/users")
    public ResponseEntity info(@RequestHeader("Authorization") String token, @RequestBody UserInfo userInfo){

        return userService.updateInfo(token,userInfo);
    }




    @GetMapping(name = "互相喜欢，喜欢，粉丝 - 统计",value = "/users/counts")
    public ResponseEntity counts() throws IOException {
        return userService.counts();
    }


    @GetMapping(name = "互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表",value = "/users/friends/{type}")
    public ResponseEntity findBytype(@PathVariable("type") String type,
                                     @RequestParam(defaultValue = "1") Integer page,
                                     @RequestParam(defaultValue = "10")Integer pagesize) throws IOException {
        return userService.findBytype(type,page,pagesize);
    }



    @DeleteMapping(name = "取消互相喜欢",value = "/users/like/{uid}")
    public ResponseEntity unlike(@PathVariable("uid") Integer uid) throws IOException {
        return userService.unlike(uid);
    }


    @PostMapping(name = "粉丝绑定好友",value = "/users/fans/{uid}")
    public ResponseEntity unfans(@PathVariable("uid") Integer uid) throws IOException {
        return userService.unfans(uid);
    }






}
